package top.shmly.universal.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import top.shmly.universal.base.common.MenuVo;
import top.shmly.universal.base.common.RoleVo;
import top.shmly.universal.base.common.SysUser;
import top.shmly.universal.base.common.UserVo;
import top.shmly.universal.config.security.AuthUser;
import top.shmly.universal.dao.mapper.SysAccountMapper;
import top.shmly.universal.dao.model.SysAccount;
import top.shmly.universal.service.UpmsService;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: universal
 * @description: 自定义用户账号密码登录认证
 * @author: Jibny
 * @date 2021-04-25 17:41:27
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysAccountMapper sysAccountMapper;

    @Resource
    private UpmsService upmsService;

    //    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        log.debug("Authenticating {}", username);
//        // user
//        UserVo userVo = upmsService.findByUsername(username);
//        if (userVo == null || userVo.getUsername() == null) {
//            log.warn("[" + username + "] 不存在!");
//            throw new UsernameNotFoundException("用户:" + username + ",不存在!");
//        }
//        Set<SimpleGrantedAuthority> grantedAuthorities = new HashSet<>();
//        // role
//        List<RoleVo> roleVoList = upmsService.getRoleByUserId(userVo.getId());
//        if (roleVoList != null && roleVoList.size() > 0) {
//            roleVoList.forEach(i -> {
//                SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_" + i.getValue());
//                grantedAuthorities.add(grantedAuthority);
//            });
//            // Permission
//            List<MenuVo> rolesPermission = upmsService.getRolesPermission(roleVoList.stream().map(RoleVo::getId).collect(Collectors.toList()));
//            if (rolesPermission != null && rolesPermission.size() > 0) {
//                rolesPermission.forEach(i -> {
//                    String url = Optional.ofNullable(i.getUrl()).orElseGet(() -> new String("不存在"));
//                    SimpleGrantedAuthority authority = new SimpleGrantedAuthority(url);
//                    grantedAuthorities.add(authority);
//                });
//            }
//        }
//        AuthUser user = new AuthUser(userVo.getUsername(), userVo.getPassword(), grantedAuthorities);
//        user.setId(userVo.getId());
//        return user;
//    }
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.debug("Authenticating {}", username);
        List<SysAccount> sysAccountList = new LambdaQueryChainWrapper<SysAccount>(sysAccountMapper).eq(SysAccount::getMobile, username).list();
        sysAccountList.forEach(System.out::println);
        if (sysAccountList == null || sysAccountList.size() == 0) {
            log.warn("[" + username + "] 不存在!");
            throw new UsernameNotFoundException(username + " 不存在!");
        }
        SysUser sysUser = new SysUser();
        sysUser.setPassword(sysAccountList.get(0).getPassword());
        sysUser.setUsername(sysAccountList.get(0).getMobile());
        Set<SimpleGrantedAuthority> authority = new HashSet<>();
        authority.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        return new AuthUser(sysUser.getUsername(), sysUser.getPassword(), authority);
    }


}
